From 91f8f7190d33df33ef52ddc55c14a4c6bc9c5ec2 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 16 Jan 2021 19:25:32 -0600 Subject: [PATCH] No point in re-testing for open-ness, due to: yield from ... --- src/pgwui_bulk_upload/exceptions.py | 10 ++++++++ src/pgwui_bulk_upload/views/bulk_upload.py | 29 +++++++++------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/pgwui_bulk_upload/exceptions.py b/src/pgwui_bulk_upload/exceptions.py index 3d953bb..d7ee256 100644 --- a/src/pgwui_bulk_upload/exceptions.py +++ b/src/pgwui_bulk_upload/exceptions.py @@ -22,6 +22,16 @@ from pgwui_core import exceptions as core_ex +# Line-by-line processing errors + +class CannotReReadError(core_ex.DataLineError): + def __init__(self, filename, lineno, exp): + super().__init__( + lineno, + f'Cannot re-open ({filename}) to read db data', + f'The error is: {exp}') + + # PGWUI setting related exceptions class Error(core_ex.Error): diff --git a/src/pgwui_bulk_upload/views/bulk_upload.py b/src/pgwui_bulk_upload/views/bulk_upload.py index 55ec65a..03a4e8b 100644 --- a/src/pgwui_bulk_upload/views/bulk_upload.py +++ b/src/pgwui_bulk_upload/views/bulk_upload.py @@ -141,33 +141,28 @@ class UploadBulkData(UploadData): trim (boolean) Trim leading and trailing whitespace? filepath Path of file (zip_root relative) - reopened The file has been re-opened after reading the header line ''' super().__init__(fileo, file_fmt, null_data, null_rep, trim=True) self.path = path self.filepath = archive_path(path) self.relation = relation - self.reopened = False def _thunk(self): '''Get the thunk which returns the next udl ''' + # Reopen the file, now that it is time to upload it try: - yield from super()._thunk() - except ValueError: - # The file isn't open - if self.reopened: - super()._thunk().close() - return # skip this file - # Reopen the file, now that it is time to upload it - self.reopened = True - try: - self.open_fileo(self.path.open('rb')) - except OSError as exp: - # If the file does not open on the next iteration it is skipped - raise ex.CannotReadError(self.filepath, exp) - next(super()._thunk()) # skip header - yield from super()._thunk() + self.open_fileo(self.path.open('rb')) + except OSError as exp: + saved = exp + + def thunk(): + raise ex.CannotReReadError(self.filepath, self.lineno, saved) + yield thunk + self.lineno -= 1 + return # shut down generator + next(super()._thunk()) # skip header + yield from super()._thunk() @attr.s -- 2.34.1